package com.example.sicnuaccountingapp.ui.budget

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.sicnuaccountingapp.base.UiState
import com.example.sicnuaccountingapp.databinding.FragmentBudgetBinding
import com.example.sicnuaccountingapp.utils.PreferencesManager
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

@AndroidEntryPoint
class BudgetFragment : Fragment() {
    private var _binding: FragmentBudgetBinding? = null
    private val binding get() = _binding!!

    private val viewModel by viewModels<BudgetViewModel>()

    @Inject
    lateinit var preferencesManager: PreferencesManager

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = FragmentBudgetBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        setupViews()
        observeState()

        viewModel.setUserId(preferencesManager.userId)
    }

    private fun setupViews() {
        setupRecyclerView()

        binding.addBudgetFab.setOnClickListener {
            showAddBudgetDialog()
        }
    }

    private fun setupRecyclerView() {

    }

    private fun observeState() {
        viewLifecycleOwner.lifecycleScope.launch {
            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.state.collect { state ->
                    when (state) {
                        is BudgetUiState.Success -> {
                            updateMonthlyBudget(state.monthlyBudget)
                        }
                        is BudgetUiState.Error -> {
                            Toast.makeText(context, state.message, Toast.LENGTH_SHORT).show()
                        }
                        is BudgetUiState.Loading -> {
                            // 可以显示加载状态
                        }
                    }
                }
            }
        }
    }

    private fun updateMonthlyBudget(monthlyBudget: MonthlyBudgetInfo) {
        binding.apply {
            monthlyBudgetAmount.text = String.format("¥%.2f", monthlyBudget.totalBudget)
            monthlyBudgetProgress.progress = monthlyBudget.progress.toInt()
            monthlyBudgetRemaining.text = String.format(
                "剩余: ¥%.2f (已使用 %.1f%%)",
                monthlyBudget.remaining,
                monthlyBudget.progress
            )
        }
    }

    private fun showAddBudgetDialog() {
        EditBudgetDialog.newInstance()
            .show(childFragmentManager, EditBudgetDialog.TAG)
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}